/**
 * 层序遍历，如果遍历到左右节点都为空的节点则退出
 * @param {TreeNode} root
 * @return {number}
 */
const minDepth = root => {
  if (!root) return 0
  let height = 0
  const queue = [root]

  while (queue.length) {
    let len = queue.length
    height += 1
    for (let i = 0; i < len; i++) {
      const node = queue.shift()
      node.left && queue.push(node.left)
      node.right && queue.push(node.right)
      if (!node.left && !node.right) {
        return height
      }
    }
  }
  return height
}

const root = {
  val: 1,
  left: {
    val: 2,
    left: null,
    right: {
      val: 5,
      left: null,
      right: null
    }
  },
  right: {
    val: 3,
    left: null,
    right: null
  }
}

console.log(minDepth(root))